package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.AutoCompleteTextField;
import javafx.scene.Node;
import javafx.util.Duration;
import javafx.util.StringConverter;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.util.JavaFXUtils;

import java.util.List;
import java.util.function.Function;

public class AutoCompleteTextFieldActions {
    static{
        PropertyFactory.regist(AutoCompleteTextField.class, "completerProperty", o -> {
            AutoCompleteTextField<?> obj = (AutoCompleteTextField<?>) o;
            return obj.completerProperty();
        });
        PropertyFactory.regist(AutoCompleteTextField.class, "completerWaitDurationProperty", o -> {
            AutoCompleteTextField<?> obj = (AutoCompleteTextField<?>) o;
            return obj.completerWaitDurationProperty();
        });
        PropertyFactory.regist(AutoCompleteTextField.class, "completerModeProperty", o -> {
            AutoCompleteTextField<?> obj = (AutoCompleteTextField<?>) o;
            return obj.completerModeProperty();
        });
        PropertyFactory.regist(AutoCompleteTextField.class, "resultNodeFactoryProperty", o -> {
            AutoCompleteTextField<?> obj = (AutoCompleteTextField<?>) o;
            return obj.resultNodeFactoryProperty();
        });
        PropertyFactory.regist(AutoCompleteTextField.class, "converterProperty", o -> {
            AutoCompleteTextField<?> obj = (AutoCompleteTextField<?>) o;
            return obj.converterProperty();
        });
    }

    public static void init(AutoCompleteTextField<Object> node, Thing thing, ActionContext actionContext){
        TextFieldActions.init(node, thing, actionContext);

        if(thing.valueExists("completerMode")){
            if("SEARCH_AUTOMATICALLY".equals(thing.getString("SEARCH_AUTOMATICALLY"))){
                node.setCompleterMode(AutoCompleteTextField.CompleterMode.SEARCH_AUTOMATICALLY);
            }else if("SEARCH_ON_REQUEST".equals(thing.getString("SEARCH_ON_REQUEST"))){
                node.setCompleterMode(AutoCompleteTextField.CompleterMode.SEARCH_ON_REQUEST);
            }
        }

        Function<String, List<Object>> completer = JavaFXUtils.getObject(thing, "completer", actionContext);
        if(completer != null){
            node.setCompleter(completer);
        }

        Duration completerWaitDuration = JavaFXUtils.getDuration(thing, "completerWaitDuration", actionContext);
        if(completerWaitDuration != null){
            node.setCompleterWaitDuration(completerWaitDuration);
        }

        StringConverter<Object> converter = JavaFXUtils.getObject(thing, "converter", actionContext);
        if(converter != null){
            node.setConverter(converter);
        }

        Function<Object, Node> 	resultNodeFactory = JavaFXUtils.getObject(thing, "resultNodeFactory", actionContext);
        if(resultNodeFactory != null) {
            node.setResultNodeFactory(resultNodeFactory);
        }
    }

    public static AutoCompleteTextField<Object> create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        AutoCompleteTextField<Object> node = new AutoCompleteTextField<>();
        init(node, self, actionContext);

        actionContext.peek().put("parent", node);
        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }

    public static void createCompleter(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        AutoCompleteTextField<Object> parent = actionContext.getObject("parent");

        parent.setCompleter(s -> self.doAction("query", actionContext, "value", s));
    }

    public static void createResultNodeFactory(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        AutoCompleteTextField<Object> parent = actionContext.getObject("parent");

        parent.setResultNodeFactory(s -> self.doAction("createNode", actionContext, "value", s));
    }
}
